package cn.rdq.iknessplatform.task;


import cn.rdq.common.constant.RedisConst;
import cn.rdq.common.constant.SysCurrentInfo;
import cn.rdq.common.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;

import java.util.Date;

@Configuration
@EnableScheduling
public class OnlineViewsScheduleTask implements SchedulingConfigurer {

    private final String cron = "0/30 * * * * ?";

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
        scheduledTaskRegistrar.addTriggerTask(() -> {
            //定时更新在线人数
            System.out.println(new Date() + ",当前在线人数: " + SysCurrentInfo.getOnline());
            redisUtil.set(RedisConst.ONLINE_KEY, SysCurrentInfo.getOnline());
        }, triggerContext -> new CronTrigger(cron).nextExecutionTime(triggerContext));
    }
}
